part of 'component.dart';

class SelectTagView extends StatefulWidget {
  const SelectTagView({
    Key? key,
    required this.items,
    this.width,
    this.onTap,
    this.tagBgColor,
    this.selectedTagBgColor,
    this.textStyle,
    this.selectedTextStyle,
    this.spacing,
    this.runSpacing,
    this.initIndexs,
    this.isSingleSelect = false,
    this.padding,
    this.tipBuilder,
  }) : super(key: key);
  final List<String> items;
  final double? width;
  final Function(List<int>)? onTap;
  final List<int>? initIndexs;
  final Color? tagBgColor;
  final Color? selectedTagBgColor;
  final TextStyle? textStyle;
  final TextStyle? selectedTextStyle;
  final double? spacing;
  final double? runSpacing;
  final bool isSingleSelect;
  final EdgeInsets? padding;
  final Widget? Function(int)? tipBuilder;

  @override
  State<SelectTagView> createState() => _SelectTagViewState();
}

class _SelectTagViewState extends State<SelectTagView> {
  List<int> selectedIndexs = [];

  @override
  void initState() {
    super.initState();
  }

  Widget _tag(int key, String value) {
    return GestureDetector(
      onTap: () {
        Get.log('点击---$key');
        if (widget.isSingleSelect) {
          selectedIndexs = [key];
        } else {
          if (selectedIndexs.contains(key)) {
            selectedIndexs.remove(key);
          } else {
            selectedIndexs.add(key);
          }
        }
        if (widget.onTap != null) {
          widget.onTap!(selectedIndexs);
        }
        // setState(() {});
      },
      behavior: HitTestBehavior.translucent,
      child: Container(
        padding: widget.padding ??
            EdgeInsets.symmetric(horizontal: 8.r, vertical: 6.r),
        decoration: BoxDecoration(
          color: selectedIndexs.contains(key)
              ? widget.selectedTagBgColor ?? ITheme.selectedColor.withAlpha(50)
              : widget.tagBgColor ?? const Color(0xffF5F5F5),
          borderRadius: BorderRadius.circular(3),
        ),
        child: Text(
          value,
          textAlign: TextAlign.center,
          style: selectedIndexs.contains(key)
              ? widget.selectedTextStyle ??
                  TextStyle(color: ITheme.selectedColor, fontSize: 13.sp)
              : widget.textStyle ??
                  TextStyle(color: const Color(0xff333333), fontSize: 13.sp),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    selectedIndexs = widget.initIndexs ?? [];
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
      ),
      // height: 100.r,
      alignment: Alignment.centerLeft,
      child: isZeroList(widget.items)
          ? Container(
              height: 80.r,
              width: 100.r,
              color: Colors.amber,
            )
          : Wrap(
              spacing: widget.spacing ?? 5.r,
              runSpacing: widget.runSpacing ?? 5.r,
              children: widget.items
                  .asMap()
                  .map(
                    (key, value) {
                      Widget? tip;
                      if (widget.tipBuilder != null) {
                        tip = widget.tipBuilder!(key);
                      }
                      return MapEntry(
                          key,
                          SizedBox(
                            width: widget.width,
                            // alignment: Alignment.center,

                            child: tip == null
                                ? _tag(key, value)
                                : Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      Expanded(child: _tag(key, value)),
                                      tip,
                                    ],
                                  ),
                          ));
                    },
                  )
                  .values
                  .toList(),
            ),
    );
  }
}

class GridTagView extends StatefulWidget {
  const GridTagView({
    Key? key,
    required this.items,
    this.rowCount,
    this.height,
    this.onTap,
    this.initIndex = 0,
    this.childAspectRatio,
  }) : super(key: key);
  final List<String> items;
  final int? rowCount;
  final double? height;
  final Function(int)? onTap;
  final int initIndex;
  final double? childAspectRatio;
  @override
  State<GridTagView> createState() => _GridTagViewState();
}

class _GridTagViewState extends State<GridTagView> {
  int selectedIndex = 0;

  @override
  void initState() {
    super.initState();
    selectedIndex = widget.initIndex;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
      ),
      child: isZeroList(widget.items)
          ? null
          : GridView.count(
              crossAxisCount: widget.rowCount ?? 3,
              shrinkWrap: true,
              childAspectRatio: widget.childAspectRatio ?? 2.0,
              mainAxisSpacing: 5.r,
              crossAxisSpacing: 5.r,
              children: widget.items
                  .asMap()
                  .map(
                    (key, value) => MapEntry(
                      key,
                      GestureDetector(
                        onTap: () {
                          Get.log('点击---$key');
                          if (widget.onTap != null) {
                            widget.onTap!(key);
                          }
                          setState(() {
                            selectedIndex = key;
                          });
                        },
                        behavior: HitTestBehavior.translucent,
                        child: Container(
                          height: widget.height ?? 40.r,
                          padding: EdgeInsets.symmetric(horizontal: 5.r),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: selectedIndex == key
                                ? const Color(0xffEDFAF4)
                                : const Color(0xfff5f5f5),
                          ),
                          child: Text(
                            value,
                            textAlign: TextAlign.center,
                            style: selectedIndex == key
                                ? TextStyle(
                                    color: Colors.green[800], fontSize: 13.sp)
                                : TextStyle(
                                    color: const Color(0xff333333),
                                    fontSize: 13.sp),
                          ),
                        ),
                      ),
                    ),
                  )
                  .values
                  .toList(),
            ),
    );
  }
}
